package com.example.zhangwei.adapter;


import java.util.ArrayList;

import com.example.zhangwei.R;
import com.example.zhangwei.model.ExpandListData;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SimpleExpandListAdapter extends BaseExpandableListAdapter{
	
	private Context mContext;
	
	private ArrayList<ExpandListData> mDataList;
	
	public SimpleExpandListAdapter(Context context, ArrayList<ExpandListData> list)
	{
		mContext = context;
        mDataList = list;
	}
	
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getGroupCount() 
	{
		// TODO Auto-generated method stub
		return mDataList.size();
	}
	
	@Override
	public Object getGroup(int groupPosition) 
	{
		// TODO Auto-generated method stub
		return mDataList.get(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) 
	{
		// TODO Auto-generated method stub
		return groupPosition;
	}
	

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		GroupViewHolder viewHolder;
        if (convertView == null)
        {
        	viewHolder = new GroupViewHolder();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expand_list_items, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.expand_list_image);
            viewHolder.title = (TextView) convertView.findViewById(R.id.expand_list_text);;
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }
        
        if (isExpanded)
        {
            viewHolder.imageView.setImageResource(R.drawable.game_group_unexpanded);
        }
        else
        {
            viewHolder.imageView.setImageResource(R.drawable.game_group_expanded);
        }
        viewHolder.title.setText(mDataList.get(groupPosition).name);
		
		return convertView;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) 
	{
		// TODO Auto-generated method stub
		return mDataList.get(groupPosition).listString.length;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) 
	{
		// TODO Auto-generated method stub
		return mDataList.get(groupPosition).listString[childPosition];
	}

	
	@Override
	public long getChildId(int groupPosition, int childPosition) 
	{
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ChildViewHolder viewHolder;
        if (convertView == null)
        {
        	viewHolder = new ChildViewHolder();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expand_list_items, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.expand_list_image);
            viewHolder.name = (TextView) convertView.findViewById(R.id.expand_list_text);;
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        viewHolder.imageView.setVisibility(View.INVISIBLE);
        viewHolder.name.setText(mDataList.get(groupPosition).listString[childPosition]);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	private class GroupViewHolder
    {
        TextView title;
        ImageView imageView;
    }
    
    private class ChildViewHolder
    {
        TextView name;
        ImageView imageView;
    }

}
